package cn.netty.example3;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import lombok.NoArgsConstructor;

@NoArgsConstructor
public class EchoClientHandle extends ChannelHandlerAdapter {

    private int counter;

    static final String MESSAGE = "enjoy learning to Netty.$_";

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {


        System.out.println("Server Response Message:"+msg+" Response Times:" + ++counter);


    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        for (int i = 0; i < 10; i++) {
            ctx.writeAndFlush(Unpooled.copiedBuffer(MESSAGE.getBytes()));
        }
        
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("client is error :" + cause.getMessage());
        ctx.close();
    }
}
